import ActionShadow from "./ActionShadow";
const { ccclass } = cc._decorator;

@ccclass
export default class Helloworld extends cc.Component {
    heroNode: cc.Node = null;
    heroSpine: sp.Skeleton = null;
    heroShadow: ActionShadow = null;

    start() {
        this.heroNode = this.node.getChildByName('Hero');
        this.heroSpine = this.heroNode.getChildByName('Model').getComponent(sp.Skeleton);
        this.heroShadow = this.heroSpine.node.getComponent(ActionShadow);
        this.node.on(cc.Node.EventType.TOUCH_START, this.heroMove, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.heroMove, this);
        let state = this.heroSpine.getState();
        let aniList = state.data.skeletonData['animations'];
        this.schedule(() => {
            this.heroSpine.setAnimation(0, aniList[~~(Math.random() * aniList.length)].name, true);
            this.heroShadow.shadowColor = cc.color(Math.random() * 255, Math.random() * 255, Math.random() * 255);
        }, 3);
    }

    private heroMove(event: cc.Event.EventTouch) {
        let pos = this.heroNode.parent.convertToNodeSpaceAR(event.getLocation());
        this.heroNode.x = pos.x;
        this.heroNode.y = pos.y;
    }
}